<?php
/**
 ***********************************************************************************************************************
 * 已维护的快递品牌服务
 ***********************************************************************************************************************
 */
namespace soa\express\express;

/**
 * Class brand
 * @package soa\express\express
 */
class brand
{
    /**
     * @var array
     */
    private $brands = [
        'sn'=>['brand' => 'sn','pinyin' => 'suning','name' => '苏宁快递','short_name' => '苏宁'],
        'gt' => ['brand' => 'gt','pinyin' => 'guotong','name' => '国通快递','short_name' => '国通'],
        'ht' => ['brand' => 'ht','pinyin' => 'huitong','name' => '汇通快运','short_name' => '汇通'],
        'hq' => ['brand' => 'hq','pinyin' => 'huiqiang','name' => '汇强快递', 'short_name' => '汇强'],
        'jd' => ['brand' => 'jd','pinyin' => 'jingdong','name' => '京东快递', 'short_name' => '京东'],
        'kj' => ['brand' => 'kj','pinyin' => 'kuaijie','name' => '快捷速递', 'short_name' => '快捷'],
        'dhl' => ['brand' => 'dhl','pinyin' => 'dhl','name' => 'DHL快递', 'short_name' => 'DHL'],
        'ups' => ['brand' => 'ups','pinyin' => 'ups','name' => 'UPS快递', 'short_name' => 'UPS'],
        'lb' => ['brand' => 'lb','pinyin' => 'longbang','name' => '龙邦速递', 'short_name' => '龙邦'],
        'fedexInter' => ['brand' => 'fedex','short' => 'lb','pinyin' => 'lianbangguoji','name' => '联邦国际', 'short_name' => '联邦国际'],
        'fedex' => ['brand' => 'fedex','short' => 'lb','pinyin' => 'lianbangguonei','name' => '联邦国内', 'short_name' => '联邦国内'],
        'lht' => ['brand' => 'lht','pinyin' => 'lianhaotong','name' => '联昊通', 'short_name' => '联昊通'],
        'nd' => ['brand' => 'nd','pinyin' => 'nengda','name' => '能达速递', 'short_name' => '能达'],
        'qy' => ['brand' => 'qy','pinyin' => 'quanyi','name' => '全一快递', 'short_name' => '全一'],
        'qf' => ['brand' => 'qf','pinyin' => 'quanfeng','name' => '全峰快递', 'short_name' => '全峰'],
        'qrt' => ['brand' => 'qrt','pinyin' => 'quanritong','name' => '全日通', 'short_name' => '全日通'],
        'rfd' => ['brand' => 'rfd','pinyin' => 'rufengda','name' => '如风达', 'short_name' => '如风达'],
        'sto' => ['brand' => 'sto','short' => 'sht','pinyin' => 'shentong','name' => '申通快递', 'short_name' => '申通'],
        'sf' => ['brand' => 'sf','short' => 'shf','pinyin' => 'shunfeng','name' => '顺丰速运', 'short_name' => '顺丰'],
        'se' => ['brand' => 'se','pinyin' => 'suer','name' => '速尔快递', 'short_name' => '速尔'],
        'tt' => ['brand' => 'tt','pinyin' => 'tiantian','name' => '天天快递', 'short_name' => '天天'],
        'yt' => ['brand' => 'yt','pinyin' => 'yuantong','name' => '圆通速递', 'short_name' => '圆通'],
        'yd' => ['brand' => 'yd','pinyin' => 'yunda','name' => '韵达快递', 'short_name' => '韵达'],
        'ys' => ['brand' => 'ys','pinyin' => 'yousu','name' => '优速物流', 'short_name' => '优速'],
        'post' => ['brand' => 'post','pinyin' => 'youzheng','name' => '邮政包裹', 'short_name' => '邮政包裹'],
        'zt' => ['brand' => 'zt','short' => 'zht','pinyin' => 'zhongtong','name' => '中通快递', 'short_name' => '中通'],
        'ems' => ['brand' => 'ems','pinyin' => 'ems','name' => 'EMS', 'short_name' => 'EMS'],
        'dp' => ['brand' => 'dp','pinyin' => 'debang','name' => '德邦物流', 'short_name' => '德邦'],
        'zjs' => ['brand' => 'zjs','short' => 'zhjs','pinyin' => 'zhaijisong','name' => '宅急送', 'short_name' => '宅急送'],
        'zy' => ['brand' => 'zy','short' => 'zy','pinyin' => 'zengyi','name' => '增益', 'short_name' => '增益'],
        'wx' => ['brand' => 'wx','short' => 'wx','pinyin' => 'wanxiang','name' => '万象', 'short_name' => '万象'],
        'dy' => ['brand' => 'dy','short' => 'dy','pinyin' => 'dayang','name' => '大洋', 'short_name' => '大洋'],
        'yc' => ['brand' => 'yc','short' => 'yc','pinyin' => 'yuanchang','name' => '远长', 'short_name' => '远长'],
        'sad' => ['brand' => 'sad','short' => 'sad','pinyin' => 'saiaodi','name' => '赛澳递', 'short_name' => '赛澳递'],
        'kr' => ['brand' => 'kr','short' => 'kr','pinyin' => 'kuanrong','name' => '宽容', 'short_name' => '宽容'],
        'city100' => ['brand' => 'city100','short' => 'city100','pinyin' => 'chengshi100','name' => '城市100', 'short_name' => '城市100'],
        'yj' => ['brand' => 'yj','short' => 'yj','pinyin' => 'yinjie','name' => '银捷', 'short_name' => '银捷'],
        'htwl' => ['brand' => 'htwl','short' => 'htwl','pinyin' => 'huitongwuliu','name' => '汇通物流', 'short_name' => '汇通物流'],
        'ytd' => ['brand' => 'ytd','short' => 'ytd','pinyin' => 'yitongda','name' => '易通达', 'short_name' => '易通达'],
        'xf' => ['brand' => 'xf','short' => 'xf','pinyin' => 'xinfeng','name' => '信丰', 'short_name' => '信丰'],
        'ane'=>['brand'=>'ane','short'=>'ane','pingyin'=>'anneng','name'=>'安能快递','short_name'=>'安能'],
        'anlx'=>['brand'=>'anlx','short'=>'anlx','pingyin'=>'angang','name'=>'鞍钢快递','short_name'=>'鞍钢'],
        'balunzhi'=>['brand'=>'balunzhi','short'=>'balunzhi','pingyin'=>'balunzhi','name'=>'巴伦支','short_name'=>'巴伦支'],
        'birdex'=>['brand'=>'ane','birdex'=>'ane','pingyin'=>'benniaohaitao','name'=>'笨鸟海淘','short_name'=>'笨鸟海淘'],
        'bqxhm'=>['brand'=>'bqxhm','short'=>'bqxhm','pingyin'=>'beiqingxiaohongmao','name'=>'北青小红帽','short_name'=>'北青小红帽'],
        'chukou1'=>['brand'=>'chukou1','short'=>'chukou1','pingyin'=>'chukouyi','name'=>'出口易','short_name'=>'出口易'],
        'cjsd'=>['brand'=>'cjsd','short'=>'cjsd','pingyin'=>'chengjisudi','name'=>'城际速递','short_name'=>'城际'],
        'coe'=>['brand'=>'coe','short'=>'coe','pingyin'=>'coe','name'=>'COE','short_name'=>'COE'],
        'fbkd'=>['brand'=>'fbkd','short'=>'fbkd','pingyin'=>'feibaokuaidi','name'=>'飞豹快递','short_name'=>'飞豹'],
        'fyps'=>['brand'=>'fyps','short'=>'fyps','pingyin'=>'feiyuanpeisong','name'=>'飞远配送','short_name'=>'飞远'],
        'gksd'=>['brand'=>'gksd','short'=>'gksd','pingyin'=>'gangkuisudi','name'=>'港快速递','short_name'=>'港快'],
        'hlwl'=>['brand'=>'hlwl','short'=>'hlwl','pingyin'=>'hengluwuliu','name'=>'恒路物流','short_name'=>'恒路'],
        'hmjkd'=>['brand'=>'hmjkd','short'=>'hmjkd','pingyin'=>'huangmajiakuaidi','name'=>'黄马甲快递','short_name'=>'黄马甲'],
        'hwps'=>['brand'=>'hwps','short'=>'hwps','pingyin'=>'huiwenpeisong','name'=>'汇文配送','short_name'=>'汇文'],
        'hywl'=>['brand'=>'hywl','short'=>'hywl','pingyin'=>'huayvwuliu','name'=>'华宇物流','short_name'=>'华宇'],
        'jet'=>['brand'=>'jet','short'=>'jet','pingyin'=>'jietekuaidi','name'=>'捷特快递','short_name'=>'捷特'],
        'jgsd'=>['brand'=>'jgsd','short'=>'jgsd','pingyin'=>'jingguangsudi','name'=>'京广速递','short_name'=>'京广'],
        'jymkd'=>['brand'=>'jymkd','short'=>'jymkd','pingyin'=>'jiayunmei','name'=>'加运美','short_name'=>'加运美'],
        'jywl'=>['brand'=>'jywl','short'=>'jywl','pingyin'=>'jiayiwuliu','name'=>'佳怡物流','short_name'=>'佳怡'],
        'lijisong'=>['brand'=>'lijisong','short'=>'lijisong','pingyin'=>'lijisong','name'=>'立即送','short_name'=>'立即送'],
        'mdm'=>['brand'=>'mdm','short'=>'mdm','pingyin'=>'menduimen','name'=>'门对门','short_name'=>'门对门'],
        'rrs'=>['brand'=>'rrs','short'=>'rrs','pingyin'=>'ririshunwuliu','name'=>'日日顺物流','short_name'=>'日日顺'],
        'sbwl'=>['brand'=>'sbwl','short'=>'sbwl','pingyin'=>'shèngbangwuliu','name'=>'晟邦物流','short_name'=>'晟邦'],
        'sevendays'=>['brand'=>'sevendays','short'=>'sevendays','pingyin'=>'qitianliansuowuliu','name'=>'7天连锁物流','short_name'=>'7天物流'],
        'sjfd'=>['brand'=>'sjfd','short'=>'sjfd','pingyin'=>'shunjiefengda','name'=>'顺捷丰达','short_name'=>'顺捷丰达'],
        'stewl'=>['brand'=>'stewl','short'=>'stewl','pingyin'=>'shentongewuliu','name'=>'申通e物流','short_name'=>'申通e物流'],
        'stwl'=>['brand'=>'stwl','short'=>'stwl','pingyin'=>'sutengkuaidi','name'=>'速腾快递','short_name'=>'速腾'],
        'tnt'=>['brand'=>'tnt','short'=>'tnt','pingyin'=>'tnt','name'=>'TNT','short_name'=>'TNT'],
        'ycwl'=>['brand'=>'ycwl','short'=>'ycwl','pingyin'=>'yuanchengkuaiyun','name'=>'远成快运','short_name'=>'远成快运'],
        'ytkd'=>['brand'=>'ytkd','short'=>'ytkd','pingyin'=>'yuntongzhonggang','name'=>'运通中港','short_name'=>'运通中港'],
        'zmkm'=>['brand'=>'zmkm','short'=>'zmkm','pingyin'=>'zhimakaimen','name'=>'芝麻开门','short_name'=>'芝麻开门'],
        'ztowl'=>['brand'=>'ztowl','short'=>'ztowl','pingyin'=>'zhitongwuliu','name'=>'直通物流','short_name'=>'直通'],
        'pj'=>['brand'=>'pj','short'=>'pj','pingyin'=>'pinjunkuaidi','name'=>'品俊快递','short_name'=>'品俊']
    ];

	/**
	 * @var array
	 */
    private $brandInfo = [
		"gt" => [ "name" => "国通快递", "tel" => "4001111123", "url" => "www.gto365.com", ],
		"ht" => [ "name" => "汇通快运", "tel" => "4009565656", "url" => "www.htky365.com", ],
		"hq" => [ "name" => "汇强快递", "tel" => "4000000177", "url" => "www.hq-ex.com", ],
		"jd" => [ "name" => "京东快递", "tel" => "4006033600", "url" => "jd-ex.jd.com", ],
		"kj" => [ "name" => "快捷快递", "tel" => "4008333666", "url" => "www.kjkd.com", ],
		"dp" => [ "name" => "德邦物流", "tel" => "95353", "url" => "www.deppon.com", ],
		"dhl" => [ "name" => "DHL快递", "tel" => "8008108000", "url" => "www.cn.dhl.com", ],
		"ups" => [ "name" => "UPS快递", "tel" => "02160511486", "url" => "www.ups.com", ],
		"lb" => [ "name" => "龙邦速递", "tel" => "02159218889", "url" => "www.lbex.com.cn", ],
		"qy" => [ "name" => "全一快递", "tel" => "4006631111", "url" => "www.unitop-apex.com", ],
		"qf" => [ "name" => "全峰快递", "tel" => "4001000001", "url" => "www.qfkd.com.cn", ],
		"qrt" => [ "name" => "全日通", "tel" => "02086298988", "url" => "www.at-express.com", ],
		"rfd" => [ "name" => "如风达", "tel" => "4000106660", "url" => "www.rufengda.com", ],
		"sto" => [ "name" => "申通快递", "tel" => "4008895543", "url" => "www.sto.cn", ],
		"sf" => [ "name" => "顺丰速运", "tel" => "4008111111", "url" => "www.sf-express.com", ],
		"sr" => [ "name" => "速尔快递", "tel" => "4001589888", "url" => "www.sure56.com", ],
		"tt" => [ "name" => "天天快递", "tel" => "4001888888", "url" => "www.ttkdex.com", ],
		"yt" => [ "name" => "圆通速递", "tel" => "02169777888", "url" => "www.yto.net.cn", ],
		"yd" => [ "name" => "韵达快递", "tel" => "4008216789", "url" => "www.yundaex.com", ],
		"ys" => [ "name" => "优速快递", "tel" => "4001111119", "url" => "www.uce.cn", ],
		"zt" => [ "name" => "中通速递", "tel" => "4008270270", "url" => "www.zto.cn", ],
		"ems" => [ "name" => "EMS", "tel" => "11183", "url" => "www.ems.com.cn", ],
		"zjs" => [ "name" => "宅急送", "tel" => "4006789000", "url" => "www.zjs.com.cn", ],
    ];


    /**
     * brand constructor.
     */
    public function __construct()
    {
        $this->cacheBrandData();
    }

    /**
     * @var
     */
    private $brandDate;

    /**
     * 从数据库获取品牌
     */
    private function fetchBrandData()
    {
        $this->brandDate = \artisan\db::connect('express_infomation')->table('data_source_detail')->getAll(['status' => 'Y']);

    }

    /**
     * 重新组织品牌
     */
    private function buildBrandData()
    {
        if (empty($this->brandDate) || !is_array($this->brandDate)) {
            return ;
        }

        $brands = [];
        foreach ($this->brandDate as $item) {
            $brandDate['brand'] = $item['express_brand'];
            $brandDate['short'] = $item['express_brand'];
            $brandDate['pinyin'] = $item['pinyin'];
            $brandDate['name'] = $item['name'];
            $brandDate['short_name'] = $item['short_name'];
            $brandDate['tel'] = $item['tel'];
            $brandDate['common'] = $item['ishot'];
            $brandDate['url'] = $item['officially_link'];
            $brandDate['logo_link'] = $item['logo_link'];
            $brands[] = $brandDate;
        }
        $this->brands = array_column($brands, null, 'brand');

    }

    /**
     * 缓存品牌
     */
    private function cacheBrandData()
    {
        $cacheKey = 'express:brand:company';

        $brandInfos = \cache::get($cacheKey);
        if (empty($brandInfos)) {
            $this->fetchBrandData();
            $this->buildBrandData();
            \cache::set($cacheKey, $this->brands, 3600);
        } else {
            $this->brands = $brandInfos;
        }

    }

    /**
	 * 获取快递品牌信息
	 * @param $brand
	 * @return array
	 */
	public function getBrandInfo($brand){
		if($this->brandInfo[$brand]){
			return $this->brandInfo[$brand];
		}
		return [];
	}

    /**
     * 获取指定品牌
     * @param $brand
     * @return mixed
     */
    public function getOne($brand)
    {
        return $this->brands[$brand];
	}

    /**
     * 获取所有快递品牌
     * @return array
     */
    public function getAll()
    {
        return $this->brands;
    }

    /**
     * 中文名称转换英文编码
     * @param $name
     * @return int|string
     */
    public function name2code($name)
    {
        if(empty($name) || !is_string($name)) {
            return '';
        }
        foreach($this->brands as $code => $brand) {
            if(strpos($brand['name'], $name) >= 0 && strpos($brand['name'], $name) !== false) {
                return $code;
            }
        }
        return '';
    }

    /**
     * 获取快递品牌中文名称
     * @param $brand
     * @return string
     */
    public function getName($brand)
    {
        if(empty($brand) || !is_string($brand)) {
            return '';
        }
        return isset($this->brands[$brand]['name']) ? $this->brands[$brand]['name'] : '';
    }

    /**
     * 获取快递品牌中文简称
     * @param $brand
     * @return string
     */
    public function getShortName($brand)
    {
        if(empty($brand) || !is_string($brand)) {
            return '';
        }
        return isset($this->brands[$brand]['short_name']) ? $this->brands[$brand]['short_name'] : '';
    }

    /**获取快递品牌中文拼音
     * @param $brand
     * @return string
     */
    public function getPinYin($brand)
    {
        if(empty($brand) || !is_string($brand)) {
            return '';
        }
        return isset($this->brands[$brand]['pinyin']) ? $this->brands[$brand]['pinyin'] : '';
    }

    /**
     * 获取快递品牌电话
     * @param $brand
     * @return string
     */
    public function getTel($brand)
    {
        if(empty($brand) || !is_string($brand)) {
            return '';
        }
        return isset($this->brands[$brand]['tel']) ? $this->brands[$brand]['tel'] : '';
    }

    public function search($find)
    {
        $find = trim($find);
        if ($find == '') return;

        foreach($this->brands as $key => $val) {
            $brand = $val['brand'];
            $pinyin = $val['pinyin'];
            $name = $val['name'];
            $short = $val['short_name'];
            if( (strpos($brand,$find)===0) || ( strpos($pinyin,$find)===0) || ( strpos($name,$find)===0) || ( strpos($short,$find)===0) ) {
                $data[$key] = $val;
            }
            unset($brand,$pinyin,$name);
        }
        return is_array($data) && count($data) ? $data : '';
    }
}